package com.course.business.controller.admin;

import com.course.business.controller.BaseController;
import com.course.model.dto.SectionDto;
import com.course.model.dto.PageDto;
import com.course.common.response.BaseResponse;
import com.course.model.dto.SectionPageDto;
import com.course.model.service.SectionService;
import com.course.model.util.ValidatorUtil;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/section")
public class SectionController extends BaseController{

    public static final String BUSINESS_NAME = "小节";

    @Resource
    private SectionService sectionService;

    /**
     * 列表查询
     */
    @PostMapping("/getSectionList")
    public BaseResponse list(@RequestBody SectionPageDto sectionPageDto) {
        ValidatorUtil.require(sectionPageDto.getChapterId(),"大章ID");
        ValidatorUtil.require(sectionPageDto.getCourseId(),"课程ID");
        BaseResponse response = new BaseResponse().sucess();
        sectionService.list(sectionPageDto);
        response.setData(sectionPageDto);
        return response;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody @Validated SectionDto sectionDto, BindingResult result) {
        // 保存校验
        ValidatorUtil.checkResult(result);
        BaseResponse response = new BaseResponse().sucess();
        sectionService.save(sectionDto);
        response.setData(sectionDto);
        return response;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        sectionService.delete(id);
        return response;
    }
}
